package com.bingo.config;



import com.bingo.service.IOCService;
import com.bingo.service.impl.IOCServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 25452
 */
@EnableAsync
@EnableAspectJAutoProxy
@Configuration
@ComponentScan("com.bingo.demo")
public class AnnotationConfig {
    @org.springframework.context.annotation.Bean
    public IOCService iocService(){
        return new IOCServiceImpl();
    }

	@Bean("asyncThreadPool")
	public Executor getAsyncExecutor(){
		System.out.println("asyncThreadPool init");
		Executor executor = new ThreadPoolExecutor(
				10,20,60L, TimeUnit.SECONDS
				,new ArrayBlockingQueue<>(100),new MyThreadFactory());
		return executor;
	}

	class MyThreadFactory implements ThreadFactory {
		final AtomicInteger threadNumber = new AtomicInteger(0);
		@Override
		public Thread newThread(Runnable r){
			Thread t = new Thread(r);
			t.setName("async-thread-"+threadNumber.getAndIncrement());
			t.setDaemon(true);
			return t;
		}
	}
}
